package cn.edu.tju.elm.controller;


//import cn.edu.tju.elb.service.CartItemService;
import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.User;
import cn.edu.tju.core.security.service.UserService;
import cn.edu.tju.elm.dto.CartItemDto;
import cn.edu.tju.elm.model.Cart;
import cn.edu.tju.elm.service.CartService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import cn.edu.tju.core.model.ResultCodeEnum;

@RestController
@RequestMapping("/api")
@Tag(name = "管理购物车", description = "对购物车内的商品增删改查")
public class CartController {
    
    
    
    @Autowired
    private UserService userService;
    private final CartService cartService;

    @Autowired
    public CartController(UserService userService, CartService cartService) {
        this.userService = userService;
        this.cartService = cartService;
    }

//    @Autowired
//    private CartItemService cartItemService;

    @PostMapping("/carts")
    public HttpResult<Cart> addCartItem(@RequestBody CartItemDto cartItemDto) {
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));
        
        Cart updatedCartItem = cartService.addOrUpdateCartItem(cartItemDto, currentUser);
        
        return HttpResult.success(updatedCartItem);
    }

    @GetMapping("/carts")
    public HttpResult<List<Cart>> getCartItems(@org.springframework.web.bind.annotation.RequestParam(required = false) Long businessId) {
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));
        
        List<Cart> cartItems;
        if (businessId != null) {
            // 如果提供了商家ID，则根据用户ID和商家ID查询
            cartItems = cartService.getCartItemsByUserAndBusiness(currentUser, businessId);
        } else {
            // 否则查询用户的所有购物车项
            cartItems = cartService.getCartItemsByUser(currentUser);
        }
        
        return HttpResult.success(cartItems);
    }

    @PutMapping("/carts")
    public HttpResult<Cart> updateCartItem(@RequestBody CartItemDto cartItemDto) {
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));
        
        Cart updatedCartItem = cartService.updateCartItemQuantity(cartItemDto, currentUser);
        
        return HttpResult.success(updatedCartItem);
    }

    @DeleteMapping("/carts")
    public HttpResult<?> deleteCartItem(@RequestBody CartItemDto cartItemDto) {
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));
        
        if (cartItemDto.getFood() == null || cartItemDto.getFood().getId() == null) {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND, "缺少必要参数: food.id");
        }
        
        cartService.deleteCartItem(cartItemDto.getFood().getId(), currentUser);
        
        return HttpResult.success("购物车项已删除");
    }
}
